package com.android.duininsuoai.adapt;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.duininsuoai.R;
import com.android.duininsuoai.activity.DirectBuyGoodsDetailActivity;
import com.android.duininsuoai.model.group.DirectBuyGoodsList;
import com.android.duininsuoai.utils.ConstantsUrl;
import com.android.duininsuoai.utils.ObjectUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/9/29.
 */

public class GridMainMarketAdapt extends BaseAdapter {
    private LayoutInflater Inflater;
    private Context content;
    private List<DirectBuyGoodsList.ListBean> listdata;

    public GridMainMarketAdapt(Context content, List<DirectBuyGoodsList.ListBean> listdata) {
        this.listdata = listdata;
        this.content = content;
        this.Inflater = LayoutInflater.from(content);
    }
    @Override
    public int getCount() {
        return listdata==null? 0:listdata.size();
    }
    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder = null;
        holder = new ViewHolder();
        if (convertView == null) {
            convertView = Inflater.inflate(R.layout.item_list_directbuy, null);
            holder.goods_list_item_image = (ImageView) convertView.findViewById(R.id.goods_list_item_image);
            holder.goods_list_item_score = (TextView) convertView.findViewById(R.id.goods_list_item_score);
            holder.goods_list_item_money = (TextView) convertView.findViewById(R.id.goods_list_item_money);
            holder.goods_list_item_title = (TextView) convertView.findViewById(R.id.goods_list_item_title);
            holder.goods_list_item_buy = (LinearLayout) convertView.findViewById(R.id.goods_list_item_buy);
            holder.directbuy_ll = (FrameLayout) convertView.findViewById(R.id.directbuy_ll);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }
        final DirectBuyGoodsList.ListBean data = listdata.get(position);
        ObjectUtils.photo2(content, data.getThumb(), ((ImageView) holder.goods_list_item_image));
        holder.goods_list_item_score.setText(data.getScore() + "积分");
        holder.goods_list_item_money.setText( "¥" + data.getPrice());
        holder.goods_list_item_title.setText(data.getName());
        holder.directbuy_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(content, DirectBuyGoodsDetailActivity.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID,data.getId());
                content.startActivity(intent);
            }
        });

        holder.goods_list_item_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(content, DirectBuyGoodsDetailActivity.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID,data.getId());
                content.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        ImageView goods_list_item_image;
        TextView goods_list_item_score;
        TextView goods_list_item_money;
        TextView goods_list_item_title;
        LinearLayout goods_list_item_buy;
        FrameLayout directbuy_ll;
    }
}
